// pages/commodity/index.js
const {
  get,
  post,
  globalData
} = getApp();
Page({
  data: {
    goodsInfo: {},
    imageList: [],
    swipeList: [],
    specifications: {
      goodsInfo: {
        name: "",
        price: 0,
        sub_name: "",
        original: "",
        value: 1,
        unique_id: ""
      },
      productVOList: []
    }, //规格
    cartType: "cart",
    cartNum: 0,
    show: false,
    showPage: false
  },
  // 初始化加载
  onLoad: function (e) {
    if (e.hasOwnProperty("share_member")) {
      globalData.share_member = e.share_member;
    };
    get(`/goods/details`, {
      unique_id: e.unique_id
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          goodsInfo: res.data.goodsVO,
          imageList: res.data.goodsIntroVOList,
          swipeList: res.data.goodsGalleryList,
          specifications: {
            goodsInfo: {
              name: res.data.goodsVO.name,
              price: res.data.productVOList[0].price,
              specs: res.data.productVOList[0].specs,
              original: res.data.goodsVO.original,
              value: 1,
              unique_id: res.data.productVOList[0].unique_id,
              enable_store: res.data.productVOList[0].enable_store
            },
            productVOList: res.data.productVOList
          },
          cartNum: res.data.cartNum,
          showPage: true
        })
      };
    })
  },
  // 选规格
  changeUnique_id(e) {
    this.setData({
      "specifications.goodsInfo.unique_id": e.currentTarget.dataset.unique_id,
      "specifications.goodsInfo.specs": e.currentTarget.dataset.specs,
      "specifications.goodsInfo.price": e.currentTarget.dataset.price,
      "specifications.goodsInfo.enable_store": e.currentTarget.dataset.enable_store,
    })
  },
  // 关闭规格
  onClose() {
    this.setData({
      show: false
    });
  },
  // 展示规格
  onClickButton(e) {
    this.setData({
      show: true,
      cartType: e.currentTarget.dataset.type
    });
  },
  // 跳转页面
  goToUrl(e) {
    wx.switchTab({
      url: e.currentTarget.dataset.url
    });
  },
  // 改变值
  changeEnable_store(e) {
    this.setData({
      "specifications.goodsInfo.value": e.detail,
    })
  },
  // 提交订单页面
  goToPaymentOrder: function (e) {
    console.log(e);
    const {
      unique_id,
      value
    } = this.data.specifications.goodsInfo;
    if (this.data.cartType == "cart") {
      post("/cart/add-product", {
        unique_id,
        num: value
      }).then(res => {
        if (res.result == 1) {
          wx.showToast({
            title: res.message,
            icon: 'success',
            duration: 2000
          });
          this.setData({
            show: false,
            cartNum: this.data.cartNum += 1
          });
        };
      });
    } else {
      if (e.detail.errMsg == "getUserInfo:ok") {
        get('/mine/user-info', e.detail.userInfo).then(res => {
          if (res.result == 1) {
            wx.navigateTo({
              url: `../paymentOrder/index?unique_id=${unique_id}&value=${value}`
            })
          }
        })
      }
    }
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})